if (!Util) {
    var Util = {};
}
//Radio button/Checkbox
Util.SelectButton = function(container, data, callbacks, config){
    this.container = container[0] ? container : $(container);
    this.el;
    this.data = data;
    this.txt = data.txt;
    this.val = data.val;
    this.title=data.title;
    this.isSelected = data.isSelected?data.isSelected:false;
    this.cb = {
        onClick: function(value, item){
        },
        afterSelect: function(value, item){
        },
        afterInverse: function(value, item){
        }
    };
    $.extend(this.cb, callbacks);
    this.cfg = {
        type: 'radio', //'checkbox'
        withGroup: false,
        disable: false
    };
    $.extend(this.cfg, config);
    this.className = this.cfg.type == 'radio' ? 'rtq-radio' : 'rtq-checkbox';
    this.html = '';
    this.init();
};
Util.SelectButton.prototype = {
    init: function(){
        var a = document.createElement('a');
        a.innerHTML = this.txt;
        a.className = this.className;
        if (this.title && this.title != '') {
            a.setAttribute('title',this.title);
        }else if(this.cfg.needTitle){
            a.setAttribute('title',this.txt);
        }
        var self = this;
        this.el = $(a).click(function(e){
            if (self.cfg.disable) {
                return;
            }
            if (!self.cfg.withGroup) {
                if (self.isSelected) {
                    self.inverse();
                }
                else {
                    self.select();
                }
            }
            self.cb.onClick(self.val, self);
        });
        if (this.cfg.disable) {
            this.disable(true);
        }
        if (this.isSelected){
            this.select();
        }
        this.container.append(a);
    },
    disable: function(isDiasble){
        this.cfg.disable = isDiasble === true ? true : false;
        if (this.cfg.disable) {
            this.el.addClass('rtq-btn-disable');
        }
        else {
            this.el.removeClass('rtq-btn-disable');
        }
    },
    select: function(){
        this.el.addClass(this.className + '-on');
        this.isSelected = true;
        this.cb.afterSelect(this.val, this);
    },
    inverse: function(){
        this.el.removeClass(this.className + '-on');
        this.isSelected = false;
        this.cb.afterInverse(this.val, this);
    },
    clear: function(){
        this.el.unbind("click");
        this.cfg.afterInverse = this.cfg.afterSelect = this.cfg.onClick = null;
        this.data = null;
        this.el.remove();
    }
};

Util.ButtonGroup = function(container, data, callbacks, config){
    this.container = container[0] ? container : $(container);
    this.el;
    this.data = data;
    this.btns = {};
    this.cb = {
        onClick: function(value, item){
        }
    };
    $.extend(this.cb, callbacks);
    this.cfg = {
        type: 'radio', //'checkbox'
        multiSelect: false,
        inverse: true,
        vertical: false,
        disable: false,
        needTitle:false,
        defaultSelected:[]
    };
    $.extend(this.cfg, config);
    this.cfg.multiSelect = this.cfg.type == 'checkbox' ? true : false;
    this.init();
};
Util.ButtonGroup.prototype = {
    init: function(){
        this.el = $('<div class="rtq-btngroup' + (this.cfg.vertical ? ' rtq-btngroup-v' : '') + '"></div>').appendTo(this.container);
        this.bindData(this.data);
        if (this.cfg.disable) {
            this.disable(true);
        }
    },
    bindData: function(data){
        var self = this;
        this.data = data;
        this.el.empty();
        this.btns = {};
        for (var i = 0; i < data.length; i++) {
            this.btns[data[i].val] = new Util.SelectButton(this.el, {
                txt: data[i].txt,
                val: data[i].val,
                isSelected:$.inArray(data[i].val,this.cfg.defaultSelected)!=-1?true:false,
                title:data[i].title
            }, {
                onClick: function(v, btn){
                    self._onClick(v, btn);
                }
            }, {
                type: this.cfg.type,
                withGroup: true,
                needTitle:this.cfg.needTitle
            });
            if (typeof(data[i].checked) != "undefined" && data[i].checked == true) {
                this.btns[data[i].val].select();
            }
        }
    },
    _onClick: function(v, btn){
        var isSelf = btn.isSelected;
        if (!this.cfg.multiSelect) {
            for (var k in this.btns) {
                this.btns[k].inverse();
            }
        }
        if (this.cfg.inverse) {
            if (isSelf) {
                btn.inverse();
            }
            else {
                btn.select();
            }
        }
        else {
            btn.select();
        }
        this.cb.onClick(v, btn);
    },
    disable: function(isDiasble){
        this.cfg.disable = isDiasble === true ? true : false;
        for (var k in this.btns) {
            this.btns[k].disable(this.cfg.disable);
        }
    },
    setValue:function(selectedArray){
        this.cfg.defaultSelected=selectedArray.slice();
        this.bindData(this.data);
    },
    getValue: function(){
        var v = [];
        for (var k in this.btns) {
            if (this.btns[k].isSelected) {
                v.push(this.btns[k].val);
            }
        }
        return v;
    },
    clear: function(){
        if (this.btns) {
            for (var o in this.btns) {
                if (typeof(this.btns[o].clear) == "function") {
                    this.btns[o].clear();
                }
            }
        }
        this.el = null, this.data = null;
    }
};